package com.mew.league.controller;

import com.mew.league.message.CommonResponse;
import com.mew.league.service.ILeagueService;
import com.mew.league.service.IMemberService;
import com.mew.common.entity.league.League;
import com.mew.common.exception.ResponseCode;
import com.mew.common.util.RandomIdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/league")
public class LeagueController {

    @Autowired
    private ILeagueService leagueService;
    @Autowired
    private IMemberService memberService;


    @PostMapping("/createLeague")
    @ResponseBody
    public CommonResponse createLeague(long uid,String name,Integer joinMode, Integer lowestVip){

        //创建人是否有联盟
        if (leagueService.inLeague(uid)){
            return new CommonResponse().buildByResponseCode(ResponseCode.InLeague);
        }

        String code = RandomIdUtil.getUUID().substring(0, 8);
        League league = League.builder().name(name).joinMode(joinMode).lowestVip(lowestVip).code(code).build();
        //宝箱刷新时间
        try {
            return leagueService.createLeague(uid,league);
        } catch (Exception e) {
            return new CommonResponse().buildByResponseCode(ResponseCode.CreateLeague);
        }
    }

    /**
     * 推荐联盟列表
     */

    @GetMapping("/getLeagues")
    @ResponseBody
    public List<League> getLeagues(String name){
        return leagueService.getLeagues(name);
    }

    @PostMapping("/applicant")
    @ResponseBody
    public CommonResponse applicant(long uid,int leagueId){
        /**
         * uid以后传进来
         */

        return leagueService.applicant(uid,leagueId);
    }
}
